package com.shunhao.widgets

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.kennyc.view.MultiStateView
import com.kennyc.view.MultiStateView.ViewState
import com.shunhao.listener.OnClickListener2
import com.shunhao.listener.SimpleListener
import com.shunhao.rxlibrary.R

/**
 * @author CuiZhen
 * @date 2019/5/25
 * QQ: 302833254
 * E-mail: goweii@163.com
 * GitHub: https://github.com/goweii
 */
object MultiStateUtils {

    fun toLoading(view: MultiStateView) {
        view.viewState = ViewState.LOADING
    }

    fun toEmpty(view: MultiStateView) {
        view.viewState = ViewState.EMPTY
    }

    fun toEmptyWithText(view: MultiStateView, text: String) {
        val empty: View? = view.getView(ViewState.EMPTY)
        val mTvMsg: TextView? = empty?.findViewById<TextView>(R.id.tv_msg)
        mTvMsg?.let {
            mTvMsg.text = text
        }
        view.viewState = ViewState.EMPTY
    }

    fun toEmptyWithTextImg(view: MultiStateView, text: String, id: Int) {
        val empty: View? = view.getView(ViewState.EMPTY)
        val mTvMsg: TextView? = empty?.findViewById<TextView>(R.id.tv_msg)
        val mIvImg: ImageView? = empty?.findViewById<ImageView>(R.id.iv_img)
        mTvMsg?.let {
            mTvMsg.text = text
        }
        mIvImg?.let {
            it.setImageDrawable(it.context.getDrawable(id))
        }
        view.viewState = ViewState.EMPTY
    }

    fun toError(view: MultiStateView) {
        view.viewState = ViewState.ERROR
    }

    fun toContent(view: MultiStateView) {
        view.viewState = ViewState.CONTENT
    }

    fun setEmptyAndErrorClick(view: MultiStateView, listener: SimpleListener) {
        setEmptyClick(view, listener)
        setErrorClick(view, listener)
    }

    private fun setEmptyClick(view: MultiStateView, listener: SimpleListener) {
        val empty = view.getView(ViewState.EMPTY)
        empty?.setOnClickListener(object : OnClickListener2() {
            override fun onClick2(v: View?) {
                listener.onResult()
            }
        })
    }

    private fun setErrorClick(view: MultiStateView, listener: SimpleListener) {
        val error = view.getView(ViewState.ERROR)
        error?.setOnClickListener(object : OnClickListener2() {
            override fun onClick2(v: View?) {
                listener.onResult()
            }
        })
    }
}